<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

class AuthController extends Controller
{
	public function __construct()
	{
		// 除login外都需要验证
		$this->middleware('auth:api', ['except' => ['login']]);
	}
	//登录获取token
    public function login(){
		$credentials = request(['email','password']);
		if (!$token = auth('api')->attempt($credentials)) {
			return $this->response->errorUnauthorized('帐号或密码错误');
		}
		return $this->respondWithToken($token);
	}
	//销毁token
	public function logout(){
		auth('api')->logout();
		return response()->json(['message' => 'Successfully logged out']);
	}
	//获取用户信息
	public function me(){
		return auth('api')->user ();
	}
	protected function respondWithToken($token)
	{
		return [
			'access_token' => $token,
			'token_type' => 'bearer',
			'expires_in' => auth('api')->factory()->getTTL() * 60,
		];
	}
}
